package kr.co.redcircle.service.oAuth.model;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;


public class SNSServiceBuilder {

	public static final String DAUM = "daum.yozm";
	public static final String FACEBOOK = "facebook";
	public static final String YAHOO_FLICKR = "yahoo.flickr";
	public static final String NAVER_ME2DAY = "naver.me2day";
	public static final String NAVER_CAFE = "naver.cafe";
	public static final String NATE_CLOG = "nate.clog";
	public static final String TWITTER = "twitter";
	public static final String VIMEO = "vimeo";
	public static final String WEIBO = "sina.weibo";
	public static final String GOOGLE_YOUTUBE = "google.youtube";
	public static final String DAUM_TISTORY = "daum.tistory";
	public static final String COMMAND_POST_THREAD = "post.thread";
	
	private static SAXBuilder builder = new SAXBuilder();
    private static Document document = null;
    
    private static Hashtable<String, SNSModel> internalCollection = new Hashtable<String, SNSModel>();

	static{
		try {
			document = builder.build("C:/Users/john/workspaces/bizBroker.0.0.0.1/ExampleBizBrokerWASClient/WebContent/WEB-INF/businessConfig.xml");
			buildModels(); 
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static SNSModel getInstance(String sns_id) {
		return internalCollection.get(sns_id);
	}

	private static void buildModels() {
		Element modulesElement = null;
		
		try {
			modulesElement = (Element)XPath.selectSingleNode(document, "//collection[@id='sns.modules']");
		} catch (JDOMException e) {
			e.printStackTrace();
		}

		if (modulesElement != null){
			
			List<Element> childModuleElement = modulesElement.getChildren();
			for (Element targetElement : childModuleElement){
				buildModule(targetElement);
			}
			
		}
		
	}

	private static void buildModule(Element moduleElement) {
		
		SNSModel model = new SNSModel();
		String mID = moduleElement.getAttributeValue("id");
		String processorClassName = moduleElement.getAttributeValue("class");
		String oauthVersion = moduleElement.getAttributeValue("ver");
			
		SNSAuthInformation auth = new SNSAuthInformation(moduleElement);
		SNSCommandCollection commCollection = new SNSCommandCollection(moduleElement);
		
		model.setAuthInfo(auth);
		model.setCommandCollection(commCollection);
		model.setProcessorclass(processorClassName);
		model.setId(mID);
		model.setVer(oauthVersion);
		
		internalCollection.put(mID,  model);
		
	}

}
